﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore.I_DAL;


namespace STHPMS1.Web.W_Controller.Decision.Preventive
{
    [RoutePrefix("api/projrecom")]
    public class DP_ProjrecomController : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetProjrecomByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string anniname=null,string analy_year = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<Dp_Preven_Detail, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
            {
                exp = pre =>pre.Anni_Name==anniname.ToString() && pre.Analy_Year == analy_year.ToString() && pre.Secon_Measure == 1;
            }
            else
            {
                exp = pre => pre.Secon_Measure == 1;
            }

            var data = this.Scene.Bll.Dp_Preven_Detail.Get<Dp_Preven_Detail>(exp, info).OrderBy(q=>q.Mainsegid).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }
        /// <summary>
        /// 自定义权重和分值
        /// </summary>
        [Route("")]
        public void Post()
        {
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            string Rdwayid = HttpContext.Current.Request.Form["Rdwayid"];
            string Meas_Ini = HttpContext.Current.Request.Form["Meas_Ini"];

            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            dao.Delete<Dp_Sejud_Calcu>(p => p.Rdwayid == Rdwayid && p.Secon_Measure == Meas_Ini);


            Dp_Sejud_Calcu info = new Dp_Sejud_Calcu();
            string CL_Coe = HttpContext.Current.Request.Form["CL_Coe"];
            string CL_Value = HttpContext.Current.Request.Form["CL_Value"];
            if (!string.IsNullOrEmpty(CL_Coe) && !string.IsNullOrEmpty(CL_Value))
            {
                info.Id= serv.NewGuid();
                info.Rdwayid = Rdwayid;
                info.Secon_Measure = Meas_Ini;
                info.Infect_Factor = "可获得的材料";
                info.Weight_Coe = Convert.ToDouble(CL_Coe);
                info.Value = Convert.ToDouble(CL_Value);
                this.Scene.Bll.Dp_Sejud_Calcu.Save(info);
            }

            string ZL_Coe = HttpContext.Current.Request.Form["ZL_Coe"];
            string ZL_Value = HttpContext.Current.Request.Form["ZL_Value"];
            if (!string.IsNullOrEmpty(ZL_Coe) && !string.IsNullOrEmpty(ZL_Value))
            {
                info.Id = serv.NewGuid();
                info.Rdwayid = Rdwayid;
                info.Secon_Measure = Meas_Ini;
                info.Infect_Factor = "施工质量";
                info.Weight_Coe = Convert.ToDouble(ZL_Coe);
                info.Value = Convert.ToDouble(ZL_Value);
                this.Scene.Bll.Dp_Sejud_Calcu.Save(info);
            }

            string NJX_Coe = HttpContext.Current.Request.Form["NJX_Coe"];
            string NJX_Value = HttpContext.Current.Request.Form["NJX_Value"];
            if (!string.IsNullOrEmpty(NJX_Coe) && !string.IsNullOrEmpty(NJX_Value))
            {
                info.Id = serv.NewGuid();
                info.Rdwayid = Rdwayid;
                info.Secon_Measure = Meas_Ini;
                info.Infect_Factor = "耐久行";
                info.Weight_Coe = Convert.ToDouble(NJX_Coe);
                info.Value = Convert.ToDouble(NJX_Value);
                this.Scene.Bll.Dp_Sejud_Calcu.Save(info);
            }

            string GY_Coe = HttpContext.Current.Request.Form["GY_Coe"];
            string GY_Value = HttpContext.Current.Request.Form["GY_Value"];
            if (!string.IsNullOrEmpty(GY_Coe) && !string.IsNullOrEmpty(GY_Value))
            {
                info.Id = serv.NewGuid();
                info.Rdwayid = Rdwayid;
                info.Secon_Measure = Meas_Ini;
                info.Infect_Factor = "交通干扰";
                info.Weight_Coe = Convert.ToDouble(GY_Coe);
                info.Value = Convert.ToDouble(GY_Value);
                this.Scene.Bll.Dp_Sejud_Calcu.Save(info);
            }

            string SSX_Coe = HttpContext.Current.Request.Form["SSX_Coe"];
            string SSX_Value = HttpContext.Current.Request.Form["SSX_Value"];
            if (!string.IsNullOrEmpty(SSX_Coe) && !string.IsNullOrEmpty(SSX_Value))
            {
                info.Id = serv.NewGuid();
                info.Rdwayid = Rdwayid;
                info.Secon_Measure = Meas_Ini;
                info.Infect_Factor = "行驶舒适性";
                info.Weight_Coe = Convert.ToDouble(SSX_Coe);
                info.Value = Convert.ToDouble(SSX_Value);
                this.Scene.Bll.Dp_Sejud_Calcu.Save(info);
            }

            string KHX_Coe = HttpContext.Current.Request.Form["KHX_Coe"];
            string KHX_Value = HttpContext.Current.Request.Form["KHX_Value"];
            if (!string.IsNullOrEmpty(KHX_Coe) && !string.IsNullOrEmpty(KHX_Value))
            {
                info.Id = serv.NewGuid();
                info.Rdwayid = Rdwayid;
                info.Secon_Measure = Meas_Ini;
                info.Infect_Factor = "抗滑性";
                info.Weight_Coe = Convert.ToDouble(KHX_Coe);
                info.Value = Convert.ToDouble(KHX_Value);
                this.Scene.Bll.Dp_Sejud_Calcu.Save(info);
            }

            string ZY_Coe = HttpContext.Current.Request.Form["ZY_Coe"];
            string ZY_Value = HttpContext.Current.Request.Form["ZY_Value"];
            if (!string.IsNullOrEmpty(ZY_Coe) && !string.IsNullOrEmpty(ZY_Value))
            {
                info.Id = serv.NewGuid();
                info.Rdwayid = Rdwayid;
                info.Secon_Measure = Meas_Ini;
                info.Infect_Factor = "噪声";
                info.Weight_Coe = Convert.ToDouble(ZY_Coe);
                info.Value = Convert.ToDouble(ZY_Value);
                this.Scene.Bll.Dp_Sejud_Calcu.Save(info);
            }

            string MG_Coe = HttpContext.Current.Request.Form["MG_Coe"];
            string MG_Value = HttpContext.Current.Request.Form["CL_Value"];
            if (!string.IsNullOrEmpty(MG_Coe) && !string.IsNullOrEmpty(MG_Value))
            {
                info.Id = serv.NewGuid();
                info.Rdwayid = Rdwayid;
                info.Secon_Measure = Meas_Ini;
                info.Infect_Factor = "美观";
                info.Weight_Coe = Convert.ToDouble(MG_Coe);
                info.Value = Convert.ToDouble(MG_Value);
                this.Scene.Bll.Dp_Sejud_Calcu.Save(info);
            }
            
        }


        /// <summary>
        /// 获取当前路线的所有初选措施：DP_PREVEN_DETAIL
        /// </summary>
        /// <param name="rdwayid"></param>
        /// <returns></returns>
        [Route("measini")]
        public dynamic GetMeas_INI(string rdwayid)
        {
            //var innerJoinQuery =
            //    from road in this.Scene.Bll.Dp_Preven_Detail.GetMeas_ini(rdwayid)
            //    select new { id = road.Key, text = road.Value };
            //return innerJoinQuery;

            var innerJoinQuery =
                from road in this.Scene.Bll.Dp_Preven_Detail.GetMeas_ini(rdwayid)
                select new { id = road, text = road };
            return innerJoinQuery;
        }

        

        /// <summary>
        /// 计算综合评判系数
        /// </summary>
        /// <param name="rdwayid"></param>
        [Route("setcoef")]
        public void GetJUDGE_COEF(string anniname, string analy_year)
        {
            this.Scene.Bll.Dp_Proj_Recom.SetJUDGE_COEF(anniname, analy_year);
        }

        /// <summary>
        /// 自动选取终选措施
        /// </summary>
        /// <param name="rdwayid"></param>
        [Route("setzxvalue")]
        public void GetSetPREVEN_MEASURE(string anniname, string analy_year)
        {
            this.Scene.Bll.Dp_Proj_Recom.SetPREVEN_MEASURE(anniname, analy_year, 1);
        }
    }
}